//
//  EditTableViewController.m
//  Navi
//
//  Created by Justsoso on 16/3/7.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "EditTableViewController.h"
#import "BIDPresident.h"
#import "PresidentDetailTableViewController.h"
@interface EditTableViewController ()<PresidentDetailTableViewControllerDelegate>

@property (nonatomic,strong) NSMutableArray *list;
@property (nonatomic,strong) NSIndexPath *curSel;

@end

@implementation EditTableViewController

- (NSMutableArray *)list
{
    if (_list == nil)
    {
        _list = [NSMutableArray array];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"Presidents" ofType:@"plist"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        NSKeyedUnarchiver *unArch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        NSArray *arr = [unArch decodeObjectForKey:@"Presidents"];
        [_list addObjectsFromArray:arr];
    }
    return _list;
}
- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    PresidentDetailTableViewController *nextVC = segue.destinationViewController;
    nextVC.delegate = self;
    nextVC.pre = self.list[self.curSel.row];
}



#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    BIDPresident *pre = self.list[indexPath.row];
    
    cell.textLabel.text = pre.name;
    
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@-%@",pre.fromYear,pre.toYear];
    // Configure the cell...
    
    return cell;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.curSel = indexPath;
    return indexPath;
}


- (void)refresh
{
    [self.tableView reloadData];
}


@end
